use axum::{Router, debug_handler};

use crate::app::{
  AppState,
  error::{ApiError, ApiResult},
  middleware::get_auth_layer,
};

mod auth;
mod user;

pub fn create_router() -> Router<AppState> {
  Router::new()
    .route("/", axum::routing::get(root))
    .nest(
      "/api",
      Router::new()
        .nest("/users", user::create_router())
        .route_layer(get_auth_layer())
        .nest("/auth", auth::create_router())
        .fallback(async || -> ApiResult<()> {
          tracing::warn!("Not found");
          Err(ApiError::NotFound)
        }),
    )
    .method_not_allowed_fallback(async || -> ApiResult<()> {
      tracing::warn!("Method not allowed");
      Err(ApiError::MethodNotAllowed)
    })
}

#[debug_handler]
async fn root() -> &'static str {
  "World, Hello!!!"
}
